package com.wct.permission.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WEI (Email:changtan20000@163.com)
 * @description
 * @date 2020/2/10 15:57
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    // jackson框架的工具类，用户转换对象为json字符串
    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     *
     * @param authentication 代表认证成功后的信息
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Map<Object, Object> result = new HashMap<>();
        result.put("success", true);

        String json = objectMapper.writeValueAsString(result);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(json);
    }
}
